package org.codingq92;

import java.lang.ref.WeakReference;

/**
 * 非静态内部类隐式持有外部类对象
 *
 * @author: CodingQ92
 * @date: 2025/7/3 22:07
 */
public class ClassGC {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner(); // 注释这行就可以让gc回收
        WeakReference<Outer> innerWeakReference = new WeakReference<>(outer);
        outer = null; // 即使外部类以及置为null，内部类对象依然存在 所以System.gc()也无法回收内部类对象
        System.gc();
        System.out.println(innerWeakReference.get() == null);
    }
}

class Outer {
    public void show() {
        System.out.println("I'm outer...");
    }

    class Inner { // 改为静态内部类也可让GC回收
        public void show() {
            System.out.println("I'm inner...");
        }
    }
}